package com.dataStructures.stack;

public class ArrayStackDemo {
	public static void main(String[] args) {
		ArrayStack stack = new ArrayStack(4);
		stack.push(1);
		stack.push(2);
		stack.push(3);
		stack.push(4);
		stack.push(5);
		stack.show();
	}
}

class ArrayStack {
	private final int maxSize;
	private final int[] stack;
	private int top = -1;

	public ArrayStack(int maxSize) {
		this.maxSize = maxSize;
		stack = new int[maxSize];
	}

	public boolean isFull() {
		return top == maxSize - 1;
	}

	public boolean isEmpty() {
		return top == -1;
	}

	public void push(int val) {
		if (isFull()) {
			System.out.println("stack full");
			return;
		}
		stack[++top] = val;
	}

	public int pop() {
		if (isEmpty()) {
			throw new RuntimeException("stack empty");
		}
		return stack[top--];
	}

	public void show() {
		if (isEmpty()) {
			System.out.println("stack empty");
			return;
		}
		for (int i = top; i >= 0; i--) {
			System.out.printf("stack[%d]=%d\n", i, stack[i]);
		}
	}
}
